package com.chalk.common.lottery;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LotteryTest {
  /*  public static void main(String[] args) {

        List<LotteryModel> gifts = new ArrayList<LotteryModel>();
        // 序号==物品Id==物品名称==概率
        gifts.add(new LotteryModel(1, "P1", "物品1", 0d));
        gifts.add(new LotteryModel(2, "P2", "物品2", 0.1d));
        gifts.add(new LotteryModel(3, "P3", "物品3", 0.5d));
        gifts.add(new LotteryModel(4, "P4", "物品4", 0.2d));
        gifts.add(new LotteryModel(5, "P5", "物品5", 0.9d));
        gifts.add(new LotteryModel(6, "P6", "物品6", 0.7d));
        gifts.add(new LotteryModel(7, "P7", "物品7", 1d));

        List<Double> orignalRates = new ArrayList<Double>(gifts.size());
        for (LotteryModel gift : gifts) {
            double probability = gift.getProbability();
            if (probability < 0) {
                probability = 0;
            }
            orignalRates.add(probability);
        }

        // statistics
        Map<Integer, Integer> count = new HashMap<Integer, Integer>();
//        for (int i = 0; i<20;i++){
//            int orignalIndex = LotteryUtil.lottery(orignalRates);
//            System.out.println(gifts.get(orignalIndex));
//        }


        double num = 100;
        for (int i = 0; i < num; i++) {
            int orignalIndex = LotteryUtil.lottery(orignalRates);

            Integer value = count.get(orignalIndex);
            count.put(orignalIndex, value == null ? 1 : value + 1);
        }
        List<Double> list = new ArrayList<>(count.size());
        for (Map.Entry<Integer, Integer> entry : count.entrySet()) {
            System.out.println(gifts.get(entry.getKey()) + ", count=" + entry.getValue() + ", probability=" + entry.getValue() / num);

            list.add(entry.getValue() / num);
        }
        for (int i = 0; i<20;i++){
            int orignalIndex = LotteryUtil.lottery(list);
            System.out.println(gifts.get(orignalIndex));
        }

    }*/
}
